﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DBUtility;
using MySql.Data.MySqlClient;

namespace web
{
    public partial class customColumn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sql1 = "select mt.columnName from menutemp mt where id =1";
            MySqlDataReader reader = DBUtility.MySqlHelper.ExecuteReader(sql1, null);
            string str ="";
            while (reader.Read())
            {
                str  = reader["columnName"].ToString();                
            }
            reader.Dispose();
            reader.Close();

            string sql = "select m.id,m.menuname,m.menulink, t.tempname1 as " + str + "  from menu m  left join menutemp mt on m.id = mt.id left join temp t on t.id = mt.id ";

            DataTable dt = DBUtility.MySqlHelper.SelectReturnDataTable(sql);
            customGridView.DataSource = dt;
            customGridView.DataBind();

            
        }

        /// <summary>
        /// 绑定生成GridView
        /// </summary>
        /// <param name="gdv">要绑定的GridView</param>
        /// <param name="dtblDataSource">GridView的数据源</param>
        /// <param name="strDataKey">GridView的DataKeyNames</param>
        public static void GridViewBind(GridView gdv, DataTable dtblDataSource, string strDataKey)
        {
            gdv.Columns.Clear();

            gdv.AutoGenerateColumns = false;
            gdv.DataSource = dtblDataSource;
            gdv.DataKeyNames = new string[]{ strDataKey };

            for (int i = 0; i < dtblDataSource.Columns.Count; i++)   //绑定普通数据列
            {
                BoundField bfColumn = new BoundField();
                bfColumn.DataField = dtblDataSource.Columns[i].ColumnName;
                bfColumn.HeaderText = dtblDataSource.Columns[i].Caption;
                gdv.Columns.Add(bfColumn);
            }

            gdv.Columns[1].Visible = false;

            CommandField cfModify = new CommandField();  //绑定命令列
            cfModify.HeaderText = "操作数据";
            cfModify.ButtonType = ButtonType.Button;
            cfModify.SelectText = "修改";
            cfModify.ShowSelectButton = true;
            gdv.Columns.Add(cfModify);

            gdv.DataBind();
        }

        
    }
}
